// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database();
const _ = db.command;
exports.main = async (event, context) => {
    try {
      const { keyword, page = 1, pageSize = 20 } = event;
      
      // 模糊搜索IP
      const ipRes = await db.collection('blindbox_ips')
        .where({
          ipName: db.RegExp({
            regexp: keyword,
            options: 'i'
          })
        })
        .get();
      
      // 模糊搜索系列
      const seriesRes = await db.collection('blindbox_series')
        .where({
          seriesName: db.RegExp({
            regexp: keyword,
            options: 'i'
          })
        })
        .get();
      
      // 模糊搜索产品
      const productRes = await db.collection('blindbox_products')
        .where({
          productName: db.RegExp({
            regexp: keyword,
            options: 'i'
          })
        })
        .get();
      
      return {
        success: true,
        data: {
          ips: ipRes.data,
          series: seriesRes.data,
          products: productRes.data
        }
      };
    } catch (error) {
      console.error('搜索失败:', error);
      return {
        success: false,
        error: error.message
      };
    }
  };